<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="CSS normalizeadding CSS Reset 在 index.css 中添加 @import-normalize; 即可。 作用是保证页面在不同浏览器上默认样式相近。">
<meta property="og:type" content="article">
<meta property="og:title" content="随手记 React 总结">
<meta property="og:url" content="http://example.com/2022/01/28/27/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="CSS normalizeadding CSS Reset 在 index.css 中添加 @import-normalize; 即可。 作用是保证页面在不同浏览器上默认样式相近。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-01-20_23-03-02.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-01-22_20-30-01.png">
<meta property="article:published_time" content="2022-01-27T16:34:27.000Z">
<meta property="article:modified_time" content="2022-02-19T16:02:58.766Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="typescript">
<meta property="article:tag" content="react">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-01-20_23-03-02.png">


<link rel="canonical" href="http://example.com/2022/01/28/27/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2022/01/28/27/","path":"2022/01/28/27/","title":"随手记 React 总结"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>随手记 React 总结 | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#CSS-normalize"><span class="nav-number">1.</span> <span class="nav-text">CSS normalize</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#SCSS"><span class="nav-number">2.</span> <span class="nav-text">SCSS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CSS-import-%E5%BC%95%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">CSS @import 引用</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#helper-scss"><span class="nav-number">3.1.</span> <span class="nav-text">helper.scss</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CSS-in-JS"><span class="nav-number">4.</span> <span class="nav-text">CSS-in-JS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E7%9B%AE%E5%BD%95%E5%A6%82%E4%B8%8B"><span class="nav-number">5.</span> <span class="nav-text">初始化目录如下</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-Router"><span class="nav-number">6.</span> <span class="nav-text">React Router</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#V6"><span class="nav-number">6.1.</span> <span class="nav-text">V6</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#V5"><span class="nav-number">6.2.</span> <span class="nav-text">V5</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE-React-Router-%E9%BB%98%E8%AE%A4%E8%B7%AF%E7%94%B1%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0-money"><span class="nav-number">7.</span> <span class="nav-text">设置 React Router 默认路由重定向到 &#x2F;money</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Hash-Router"><span class="nav-number">8.</span> <span class="nav-text">Hash Router</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BC%95%E5%85%A5-svg"><span class="nav-number">9.</span> <span class="nav-text">引入 svg</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Icon"><span class="nav-number">10.</span> <span class="nav-text">Icon</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BC%98%E5%8C%96%E4%B8%80%E4%B8%AA%E4%B8%AA%E5%AF%BC%E5%85%A5-svg"><span class="nav-number">11.</span> <span class="nav-text">优化一个个导入 svg</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9-svg-%E9%A2%9C%E8%89%B2-lt-NavLink-gt"><span class="nav-number">12.</span> <span class="nav-text">修改 svg 颜色 &lt;NavLink &gt;</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CategorySection-%E7%BB%84%E4%BB%B6%E4%BC%98%E5%8C%96"><span class="nav-number">13.</span> <span class="nav-text">CategorySection 组件优化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%90%88%E5%B9%B6-className"><span class="nav-number">14.</span> <span class="nav-text">合并 className</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%83%A8%E7%BD%B2"><span class="nav-number">15.</span> <span class="nav-text">部署</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/01/28/27/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          随手记 React 总结
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2022-01-28 00:34:27" itemprop="dateCreated datePublished" datetime="2022-01-28T00:34:27+08:00">2022-01-28</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h4 id="CSS-normalize"><a href="#CSS-normalize" class="headerlink" title="CSS normalize"></a>CSS normalize</h4><p><a target="_blank" rel="noopener" href="https://create-react-app.dev/docs/adding-css-reset/">adding CSS Reset</a></p>
<p>在 index.css 中添加 <code>@import-normalize;</code> 即可。</p>
<p>作用是保证页面在不同浏览器上默认样式相近。</p>
<span id="more"></span>

<h4 id="SCSS"><a href="#SCSS" class="headerlink" title="SCSS"></a>SCSS</h4><p><a target="_blank" rel="noopener" href="https://create-react-app.dev/docs/adding-a-sass-stylesheet/">adding Sass Stylesheets</a></p>
<p><code>yarn add node-sass@npm:sass</code></p>
<p>node-sass，下载速度慢、本地编译慢。</p>
<p>react 只支持 node-sacc 不支持 dart-sass。  </p>
<p>npm 6.9 支持一个新功能，叫做 package alias。node-sass@npm:sass</p>
<h4 id="CSS-import-引用"><a href="#CSS-import-引用" class="headerlink" title="CSS @import 引用"></a>CSS @import 引用</h4><p>Vue 项目中干用 @ 表示 src/ 目录。</p>
<p>React 直接写目录即可。</p>
<p>JS 配置：在 tsconfig.json 中添加一句 <code>&quot;baseUrl&quot;: &quot;src&quot;</code>。<a target="_blank" rel="noopener" href="https://create-react-app.dev/docs/importing-a-component/#absolute-imports">文档</a></p>
<p>在 Webstorm 中将 src 目录标记为 resource root 。</p>
<h5 id="helper-scss"><a href="#helper-scss" class="headerlink" title="helper.scss"></a>helper.scss</h5><p>创建 helper.css，helper.scss 里放置变量、函数等公用的东西。</p>
<h4 id="CSS-in-JS"><a href="#CSS-in-JS" class="headerlink" title="CSS-in-JS"></a>CSS-in-JS</h4><p><a target="_blank" rel="noopener" href="https://github.com/styled-components/styled-components">styled-components</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add styled-components</span><br><span class="line">yarn add --dev @types/styled-components</span><br></pre></td></tr></table></figure>

<p>或者用 <a target="_blank" rel="noopener" href="https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/">css-module</a></p>
<p>使用：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title class_">AppWrapper</span> = styled.<span class="property">div</span><span class="string">`</span></span><br><span class="line"><span class="string">  color: #333;</span></span><br><span class="line"><span class="string">`</span>;</span><br></pre></td></tr></table></figure>



<h4 id="初始化目录如下"><a href="#初始化目录如下" class="headerlink" title="初始化目录如下"></a>初始化目录如下</h4><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-01-20_23-03-02.png" alt="Snipaste_2022-01-20_23-03-02" style="float:left">





<h4 id="React-Router"><a href="#React-Router" class="headerlink" title="React Router"></a>React Router</h4><p>本项目一共4个页面：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#/money 记账页</span><br><span class="line">#/labels 标签页</span><br><span class="line">#/statistics 统计页</span><br><span class="line">404 页</span><br><span class="line"></span><br><span class="line">默认进入 #/money </span><br></pre></td></tr></table></figure>



<p>安装</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add react-router-dom@6</span><br><span class="line">yarn add --dev @types/react-router-dom@6</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://v5.reactrouter.com/web/guides/quick-start">文档</a></p>
<p><strong>报错：</strong> TS2305: Module ‘“react-router-dom”‘ has no exported member ‘Switch’.</p>
<p><strong>解决：</strong> Use <code>Routes</code> instead of <code>Switch</code>。</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_43392573/article/details/121658373">V5 -&gt; V6 的变化</a></p>
<p><strong>本项目使用 V6</strong></p>
<h5 id="V6"><a href="#V6" class="headerlink" title="V6"></a>V6</h5><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">Routes</span>&gt;</span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Home</span>/&gt;</span>&#125;/&gt;</span></span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/users&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Users</span>/&gt;</span>&#125;/&gt;</span></span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/about&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">About</span>/&gt;</span>&#125;/&gt;</span></span><br><span class="line">&lt;/<span class="title class_">Routes</span>&gt;</span><br></pre></td></tr></table></figure>

<h5 id="V5"><a href="#V5" class="headerlink" title="V5"></a>V5</h5><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> &lt;<span class="title class_">Switch</span>&gt;</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/about&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">About</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/users&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Users</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Home</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line">&lt;/<span class="title class_">Switch</span>&gt;</span><br></pre></td></tr></table></figure>



<h4 id="设置-React-Router-默认路由重定向到-money"><a href="#设置-React-Router-默认路由重定向到-money" class="headerlink" title="设置 React Router 默认路由重定向到 /money"></a>设置 React Router 默认路由重定向到 /money</h4><p>v6</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">Route</span> path=<span class="string">&quot;/&quot;</span> element=&#123;<span class="language-xml"><span class="tag">&lt;<span class="name">Navigate</span> <span class="attr">replace</span> <span class="attr">to</span>=<span class="string">&quot;/money&quot;</span>/&gt;</span></span>&#125;/&gt;</span><br></pre></td></tr></table></figure>

<p>v5</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">Redirect</span> exact <span class="keyword">from</span>=<span class="string">&quot;/&quot;</span> to=<span class="string">&quot;/money&quot;</span> /&gt;</span><br></pre></td></tr></table></figure>



<h4 id="Hash-Router"><a href="#Hash-Router" class="headerlink" title="Hash Router"></a>Hash Router</h4><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">BrowserRouter</span> <span class="keyword">as</span> <span class="title class_">Router</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>改为</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">HashRouter</span> <span class="keyword">as</span> <span class="title class_">Router</span>,&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span>;</span><br></pre></td></tr></table></figure>





<h4 id="引入-svg"><a href="#引入-svg" class="headerlink" title="引入 svg"></a>引入 svg</h4><p>需要配置，<a target="_blank" rel="noopener" href="https://github.com/JetBrains/svg-sprite-loader">svg-sprite-loader</a>，<a target="_blank" rel="noopener" href="https://github.com/svg/svgo-loader">svgo-loader</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yarn eject // 拿到 webpack 配置</span><br><span class="line">yarn add --dev svgo-loader</span><br><span class="line">yarn add --dev svg-sprite-loader</span><br></pre></td></tr></table></figure>

<p>在 config/webpack.config.js 中添加如下代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">test</span>: <span class="regexp">/\.svg$/</span>,</span><br><span class="line">    <span class="attr">use</span>: [</span><br><span class="line">        &#123; <span class="attr">loader</span>: <span class="string">&#x27;svg-sprite-loader&#x27;</span>, <span class="attr">options</span>: &#123;&#125; &#125;,</span><br><span class="line">        &#123; <span class="attr">loader</span>: <span class="string">&#x27;svgo-loader&#x27;</span>, <span class="attr">options</span>: &#123;&#125; &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-01-22_20-30-01.png" alt="Snipaste_2022-01-22_20-30-01" width="500px"></center>

<h4 id="Icon"><a href="#Icon" class="headerlink" title="Icon"></a>Icon</h4><p>使用：</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 直接引用即可</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;icons/money.svg&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">require</span>(<span class="string">&#x27;icons/money.svg&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">className</span>=<span class="string">&quot;icon&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">use</span> <span class="attr">xlinkHref</span>=<span class="string">&quot;#labels&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">className</span>=<span class="string">&quot;icon&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">use</span> <span class="attr">xlinkHref</span>=<span class="string">&quot;#morney&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">className</span>=<span class="string">&quot;icon&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">use</span> <span class="attr">xlinkHref</span>=<span class="string">&quot;#statistics&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<p>优化：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 优化抽离为 Icon.tsx</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;icons/money.svg&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;icons/labels.svg&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;icons/statistics.svg&#x27;</span>;</span><br><span class="line"></span><br><span class="line">type <span class="title class_">Props</span> = &#123;</span><br><span class="line">  <span class="attr">name</span>: string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Icon</span> = (<span class="params">props: Props</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">className</span>=<span class="string">&quot;icon&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">use</span> <span class="attr">xlinkHref</span>=<span class="string">&#123;</span>&#x27;#&#x27; + <span class="attr">props.name</span>&#125;/&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Icon</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用</span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">Icon</span> <span class="attr">name</span>=<span class="string">&quot;labels&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">Icon</span> <span class="attr">name</span>=<span class="string">&quot;money&quot;</span>/&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">Icon</span> <span class="attr">name</span>=<span class="string">&quot;statistics&quot;</span>/&gt;</span></span></span><br></pre></td></tr></table></figure>



<h4 id="优化一个个导入-svg"><a href="#优化一个个导入-svg" class="headerlink" title="优化一个个导入 svg"></a>优化一个个导入 svg</h4><p>在 Icon.tsx 中添加</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">importAll</span> = (<span class="params">requireContext: __WebpackModuleApi.RequireContext</span>) =&gt; requireContext.<span class="title function_">keys</span>().<span class="title function_">forEach</span>(requireContext);</span><br><span class="line"><span class="keyword">try</span> &#123;<span class="title function_">importAll</span>(<span class="built_in">require</span>.<span class="title function_">context</span>(<span class="string">&#x27;../icons&#x27;</span>, <span class="literal">true</span>, <span class="regexp">/\.svg$/</span>));&#125; <span class="keyword">catch</span> (e) &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(e); &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>报错修复：<code>yarn add --dev @types/webpack-env</code> <a target="_blank" rel="noopener" href="https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11324">文档</a></p>
<p>将 svg 抽离成 <code>&lt;Icon /&gt;</code> 组件</p>
<p>将导航栏抽离成 <code>&lt;Nav /&gt;</code> 组件</p>
<p>将布局抽离成 <code>&lt;Layout /&gt;</code> 组件</p>
<h4 id="修改-svg-颜色-lt-NavLink-gt"><a href="#修改-svg-颜色-lt-NavLink-gt" class="headerlink" title="修改 svg 颜色 &lt;NavLink &gt;"></a>修改 svg 颜色 <code>&lt;NavLink &gt;</code></h4><p><a target="_blank" rel="noopener" href="https://github.com/svg/svgo/blob/main/plugins/removeAttrs.js">文档</a></p>
<p>在 config/webpack.config.js 中：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">test</span>: <span class="regexp">/\.svg$/</span>,</span><br><span class="line">    <span class="attr">use</span>: [</span><br><span class="line">        &#123; <span class="attr">loader</span>: <span class="string">&#x27;svg-sprite-loader&#x27;</span>, <span class="attr">options</span>: &#123;&#125; &#125;,</span><br><span class="line">        &#123; <span class="attr">loader</span>: <span class="string">&#x27;svgo-loader&#x27;</span>, <span class="attr">options</span>: &#123;</span><br><span class="line">            <span class="attr">plugins</span>: [</span><br><span class="line">                &#123; <span class="attr">removeAttrs</span>: &#123; <span class="attr">attrs</span>: <span class="string">&#x27;fill&#x27;</span> &#125; &#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125; &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><a target="_blank" rel="noopener" href="https://reactrouter.com/docs/en/v6/api#navlink">NavLink</a></p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">NavLink</span> to=<span class="string">&quot;/labels&quot;</span></span><br><span class="line">         className=&#123;<span class="function">(<span class="params">&#123;isActive&#125;</span>) =&gt;</span> (isActive ? <span class="string">&#x27;active&#x27;</span> : <span class="string">&#x27;inactive&#x27;</span>)&#125;&gt;</span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">Icon</span> <span class="attr">name</span>=<span class="string">&quot;labels&quot;</span>/&gt;</span></span></span><br><span class="line">  标签</span><br><span class="line">&lt;/<span class="title class_">NavLink</span>&gt;</span><br></pre></td></tr></table></figure>







<p>非受控/受控 input</p>
<p>受控</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">NoteSection</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> [note, setNote] = <span class="title function_">useState</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">label</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">span</span>&gt;</span>备注<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;输入备注信息&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               <span class="attr">value</span>=<span class="string">&#123;note&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               <span class="attr">onChange</span>=<span class="string">&#123;e</span> =&gt;</span> setNote(e.target.value)&#125;/&gt;</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">label</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<p>非受控，使用 ref</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">NoteSection</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> [note, setNote] = <span class="title function_">useState</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">    </span><br><span class="line">  <span class="keyword">const</span> refInput = useRef&lt;<span class="title class_">HTMLInputElement</span>&gt;(<span class="literal">null</span>);</span><br><span class="line">  <span class="keyword">const</span> <span class="title function_">onBlur</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (refInput.<span class="property">current</span> !== <span class="literal">null</span>) &#123;</span><br><span class="line">      <span class="title function_">setNote</span>(refInput.<span class="property">current</span>.<span class="property">value</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">label</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">span</span>&gt;</span>备注<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;输入备注信息&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               <span class="attr">value</span>=<span class="string">&#123;note&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               <span class="attr">ref</span>=<span class="string">&#123;refInput&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               <span class="attr">onBlur</span>=<span class="string">&#123;onBlur&#125;/</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">label</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<p><strong>React onChange 会在输入一个字符的时候触发</strong></p>
<p><strong>HTML onChange 会在鼠标移走的时候触发，早于 onBlur</strong></p>
<h4 id="CategorySection-组件优化"><a href="#CategorySection-组件优化" class="headerlink" title="CategorySection 组件优化"></a>CategorySection 组件优化</h4><figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">CategorySection</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> [category, setCategory] = <span class="title function_">useState</span>(<span class="string">&#x27;-&#x27;</span>);</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">className</span>=<span class="string">&#123;category</span> === <span class="string">&#x27;-&#x27;</span> ? &#x27;<span class="attr">selected</span>&#x27; <span class="attr">:</span> &#x27;&#x27;&#125;</span></span></span><br><span class="line"><span class="tag"><span class="language-xml">            <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> setCategory(&#x27;-&#x27;)&#125;&gt;支出</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">li</span> <span class="attr">className</span>=<span class="string">&#123;category</span> === <span class="string">&#x27;+&#x27;</span> ? &#x27;<span class="attr">selected</span>&#x27; <span class="attr">:</span> &#x27;&#x27;&#125;</span></span></span><br><span class="line"><span class="tag"><span class="language-xml">            <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> setCategory(&#x27;+&#x27;)&#125;&gt;收入</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>优化后：</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">CategorySection</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> categoryMap = &#123;<span class="string">&#x27;-&#x27;</span>: <span class="string">&#x27;支出&#x27;</span>, <span class="string">&#x27;+&#x27;</span>: <span class="string">&#x27;收入&#x27;</span>&#125;;</span><br><span class="line">  <span class="keyword">type</span> <span class="title class_">Keys</span> = keyof <span class="keyword">typeof</span> categoryMap</span><br><span class="line">  <span class="keyword">const</span> [categoryList] = useState&lt;<span class="title class_">Keys</span>[]&gt;([<span class="string">&#x27;-&#x27;</span>, <span class="string">&#x27;+&#x27;</span>]);</span><br><span class="line">  <span class="keyword">const</span> [category, setCategory] = <span class="title function_">useState</span>(<span class="string">&#x27;-&#x27;</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        &#123;categoryList.map(c =&gt;</span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">li</span> <span class="attr">className</span>=<span class="string">&#123;category</span> === <span class="string">c</span> ? &#x27;<span class="attr">selected</span>&#x27; <span class="attr">:</span> &#x27;&#x27;&#125;</span></span></span><br><span class="line"><span class="tag"><span class="language-xml">              <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> setCategory(c)&#125;</span></span><br><span class="line"><span class="language-xml">          &gt;&#123;categoryMap[c]&#125;</span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        )&#125;</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Wrapper</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h4 id="合并-className"><a href="#合并-className" class="headerlink" title="合并 className"></a>合并 className</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add classnames</span><br><span class="line">yarn add --dev @types/classnames</span><br></pre></td></tr></table></figure>

<p>demo：</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> cs <span class="keyword">from</span> <span class="string">&#x27;classnames&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="title function_">importAll</span> = (<span class="params">requireContext: __WebpackModuleApi.RequireContext</span>) =&gt; requireContext.<span class="title function_">keys</span>().<span class="title function_">forEach</span>(requireContext);</span><br><span class="line"><span class="keyword">try</span> &#123;<span class="title function_">importAll</span>(<span class="built_in">require</span>.<span class="title function_">context</span>(<span class="string">&#x27;../icons&#x27;</span>, <span class="literal">true</span>, <span class="regexp">/\.svg$/</span>));&#125; <span class="keyword">catch</span> (e) &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(e); &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Props</span> = &#123;</span><br><span class="line">  name?: <span class="built_in">string</span>;</span><br><span class="line">&#125; &amp; <span class="title class_">React</span>.<span class="property">SVGAttributes</span>&lt;<span class="title class_">SVGElement</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Icon</span> = (<span class="params">props: Props</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123;name, children, className, ...rest&#125; = props;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">className</span>=<span class="string">&#123;cs(</span>&#x27;<span class="attr">icon</span>&#x27;, <span class="attr">className</span>)&#125; &#123;<span class="attr">...rest</span>&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">      &#123;props.name &amp;&amp; <span class="tag">&lt;<span class="name">use</span> <span class="attr">xlinkHref</span>=<span class="string">&#123;</span>&#x27;#&#x27; + <span class="attr">props.name</span>&#125;/&gt;</span>&#125;</span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">Icon</span>;</span><br></pre></td></tr></table></figure>





<h4 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h4><p>在 package.json 中添加</p>
<p><code>&quot;homepage&quot;: &quot;/morney-r-website&quot;,</code></p>
<p><a target="_blank" rel="noopener" href="https://create-react-app.dev/docs/deployment/#serving-the-same-build-from-different-paths">文档</a></p>
<p>再运行 <code>yarn build</code></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/typescript/" rel="tag"><i class="fa fa-tag"></i> typescript</a>
              <a href="/tags/react/" rel="tag"><i class="fa fa-tag"></i> react</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/01/17/52/" rel="prev" title="随手记 Vue 总结">
                  <i class="fa fa-chevron-left"></i> 随手记 Vue 总结
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/02/05/08/" rel="next" title="大屏可视化项">
                  大屏可视化项 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
